package com.example.rg1gameactivityapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText editTextName;
    private EditText editTextPWD;
    private Button buttonLogin;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextName=findViewById(R.id.editTextName);
        editTextPWD=findViewById(R.id.editTextPWD);
        readUserInfo();
        buttonLogin=findViewById(R.id.buttonLogin);
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,StateActivity.class);
                intent.putExtra("userName",editTextName.getText().toString());
                intent.putExtra("userPWD",editTextPWD.getText().toString());
                saveUserInfo();
                startActivity(intent);
            }
        });
    }

    private void saveUserInfo()
    {
        SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("userName",editTextName.getText().toString());
        editor.putString("userPWD",editTextPWD.getText().toString());
        editor.apply();
    }

    private void readUserInfo()
    {
        SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
        String name=sp.getString("userName","");
        String pwd=sp.getString("userPWD","");
        editTextName.setText(name);
        editTextPWD.setText(pwd);
    }
}
